package com.example.rabbitdemo.config;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class RabbitmqConfig {
    @Resource
    private ConnectionFactory connectionFactory;

    @Bean
    public RabbitAdmin rabbitAdmin(){
        RabbitAdmin admin=new RabbitAdmin(connectionFactory);
        admin.setAutoStartup(true);
        return admin;
    }

    /**
     * 交换机初始化
     *
     */
    public static final String DIRECT_EXCHANGE = "direct.exchange";
    public static final String TOPIC_EXCHANGE = "topic.exchange";
    public static final String FANOUT_EXCHANGE = "fanout.exchange";

    @Bean(name = DIRECT_EXCHANGE)
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE);
    }
    @Bean(name = TOPIC_EXCHANGE)
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }
    @Bean(name = FANOUT_EXCHANGE)
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE);
    }


    /**
     * 队列初始化
     * @return
     */
    public static final String DIRECT_QUEUE1 = "direct.queue1";
    public static final String DIRECT_QUEUE2 = "direct.queue2";

    public static final String TOPIC_QUEUE1 = "topic.queue1";
    public static final String TOPIC_QUEUE2 = "topic.queue2";

    public static final String FANOUT_QUEUE1 = "fanout.queue1";
    public static final String FANOUT_QUEUE2 = "fanout.queue2";

    @Bean(name = DIRECT_QUEUE1)
    public Queue directQueue1() {
        // durable: 是否持久化，默认是false。持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在
        // exclusive: 默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete: 是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        Queue queue=new Queue(DIRECT_QUEUE1);
        rabbitAdmin().declareQueue(queue);
        return queue;
    }

    @Bean(name = DIRECT_QUEUE2)
    public Queue directQueue2(){
        Queue queue=new Queue(DIRECT_QUEUE2);
        rabbitAdmin().declareQueue(queue);
        return  queue;
    }

    @Bean(name = TOPIC_QUEUE1)
    public Queue topicQueue1(){
        Queue queue=new Queue(TOPIC_QUEUE1);
        rabbitAdmin().declareQueue(queue);
        return  queue;
    }

    @Bean(name = TOPIC_QUEUE2)
    public Queue topicQueue2(){
        Queue queue=new Queue(TOPIC_QUEUE2);
        rabbitAdmin().declareQueue(queue);
        return  queue;
    }

    @Bean(name = FANOUT_QUEUE1)
    public Queue fanoutQueue1(){
        Queue queue=new Queue(FANOUT_QUEUE1);
        rabbitAdmin().declareQueue(queue);
        return  queue;
    }

    @Bean(name = FANOUT_QUEUE2)
    public Queue fanoutQueue2(){
        Queue queue=new Queue(FANOUT_QUEUE2);
        rabbitAdmin().declareQueue(queue);
        return  queue;
    }
    /**
     * 交换机队列绑定
     *
     * @return 返回绑定的对象
     */
    @Bean
    public Binding bindingSimpleQueue1() {
        return BindingBuilder.bind(directQueue1()).to(directExchange()).with("demo");
    }

    @Bean
    public Binding bindingSimpleQueue2() {
        return BindingBuilder.bind(directQueue2()).to(directExchange()).with("demo2");
    }

    /**
     * topic 主题订阅
     * @return
     */
    @Bean
    public Binding bindingTopicOneExchange() {
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("topic.one");
    }

    @Bean
    public Binding bindingTopicExchange() {
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("topic.#");
    }

    /**
     * 消息广播
     * @return
     */

    @Bean
    public Binding bindingFanoutOneExchange() {
        return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    }

    @Bean
    public Binding bindingFanoutExchange() {
        return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
    }









}
